//
//  PersonalHomeHeaderView.m
//  OnionHome
//
//  Created by fengjie on 2019/9/24.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "PersonalHomeHeaderView.h"
#import "EditorPersonalInformation.h"
#import "OnionCertificationController.h"

#import "MyPrivateMessageChatRoomVC.h"
#import "MyPrivateMessageModel.h"


@interface PersonalHomeHeaderView ()
/**用户头像*/
@property (nonatomic,strong)UIImageView*IconImage;
/**文章数*/
@property (nonatomic,strong)UILabel*oneNumLabel;
/**粉丝数*/
@property (nonatomic,strong)UILabel*twoNumLabel;
/**关注数*/
@property (nonatomic,strong)UILabel*thereNumLabel;
/**获赞数*/
@property (nonatomic,strong)UILabel*fourNumLabel;
/**认证*/
@property (nonatomic,strong)UILabel*certificationLabel;
/**位置*/
@property (nonatomic,strong)UILabel*locationLabel;
/**简介*/
@property (nonatomic,strong)UILabel*IntroductionLabel;
/**编辑资料或者私信按钮*/
@property (nonatomic,strong)UIButton * editorBtn;
/**认证按钮*/
@property (nonatomic,strong)UIButton * applyBtn;

/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataSoure;
@end

@implementation PersonalHomeHeaderView
-(NSMutableArray *)dataSoure{
    if (!_dataSoure) {
        _dataSoure =[NSMutableArray array];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"MyPrivateMessageList" ofType:@"plist"];
           //当数据结构为数组时
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            MyPrivateMessageModel *model = [MyPrivateMessageModel new];
            [model setValuesForKeysWithDictionary:obj];
            [_dataSoure addObject:model];
        }];
    }
    return _dataSoure;
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
        [self dataSoure];
    }
    return self;
}
-(void)setType:(PersonalHomePageSwithType)type{
    _type = type;
    if (type == PersonalHomePageMe) {
        [self.editorBtn setTitle:@"编辑资料" forState:UIControlStateNormal];
        [self.applyBtn setTitle:@"申请认证" forState:UIControlStateNormal];
    }else if (type == PersonalHomePageTA){
        [self.editorBtn setTitle:@"私信" forState:UIControlStateNormal];
        [self.applyBtn setTitle:@"认证" forState:UIControlStateNormal];
    }
}
-(void)createUI{
    
    UIView * oneView = [self createButtonView:@"文章" label:self.oneNumLabel];
    UIView * twoView = [self createButtonView:@"粉丝" label:self.twoNumLabel];
    UIView * thereView = [self createButtonView:@"关注" label:self.thereNumLabel];
    UIView * fourView = [self createButtonView:@"获赞" label:self.fourNumLabel];
    
   self.editorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.editorBtn setTitle:@"编辑资料" forState:UIControlStateNormal];
    [self.editorBtn setTitleColor:UIColorFromRGB(0x273323) forState:UIControlStateNormal];
    self.editorBtn.titleLabel.font = SYSTEMFONT(14);
    ViewBorderRadius(self.editorBtn, 0, 1, UIColorFromRGB(0x959E97));
    kWeakSelf(weakSelf);
    [self.editorBtn addActionHandler:^{
        if (weakSelf.type == PersonalHomePageMe) {
            [[weakSelf getCurrentViewController].navigationController pushViewController:[EditorPersonalInformation new] animated:YES];
        }else{
            MyPrivateMessageModel * model = weakSelf.dataSoure.lastObject;
            MyPrivateMessageChatRoomVC * vc = [MyPrivateMessageChatRoomVC new];
            vc.model = model;
            [[weakSelf getCurrentViewController].navigationController pushViewController:vc animated:YES];
        }
    }];
    
    self.applyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.applyBtn setTitle:@"申请认证" forState:UIControlStateNormal];
    self.applyBtn.titleLabel.font = SYSTEMFONT(14);
    [self.applyBtn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
    [self.applyBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x009966)] forState:UIControlStateNormal];
    [self.applyBtn addActionHandler:^{
        [[weakSelf getCurrentViewController].navigationController pushViewController:[OnionCertificationController new] animated:YES];

//        if (weakSelf.type == PersonalHomePageMe) {
//            [[weakSelf getCurrentViewController].navigationController pushViewController:[OnionCertificationController new] animated:YES];
//        }else{
//
//        }
    }];
    
    self.certificationLabel = UILabel.new;
    self.certificationLabel.text = @"认证:优质美食达人";
    self.locationLabel = UILabel.new;
    self.locationLabel.text = @"位置:深圳";
    self.IntroductionLabel = UILabel.new;
    self.IntroductionLabel.text = @"简介:每天直播，旅游，美食记录";
    
    UIView *sexView = [self createImageAndTitleView:@"" LabelName:self.certificationLabel];
    UIView *sevenView = [self createImageAndTitleView:@"" LabelName:self.locationLabel];
    UIView *eightView = [self createImageAndTitleView:@"" LabelName:self.IntroductionLabel];

    UIView * lineView = UIView.new;
    lineView.backgroundColor = UIColorFromRGB(0xF5F7F5);

    [self addSubview:self.IconImage];
    [self addSubview:oneView];
    [self addSubview:twoView];
    [self addSubview:thereView];
    [self addSubview:fourView];
    [self addSubview:self.editorBtn];
    [self addSubview:self.applyBtn];
    [self addSubview:sexView];
    [self addSubview:sevenView];
    [self addSubview:eightView];
    [self addSubview:lineView];

    
    [self.IconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(16);
        make.width.height.mas_equalTo(72);
    }];
    
    [oneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.IconImage.mas_right).offset(22);
        make.top.mas_equalTo(self.IconImage);
    }];
    [twoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(oneView.mas_right).offset(40);
        make.top.mas_equalTo(self.IconImage);
    }];
    [thereView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(twoView.mas_right).offset(40);
        make.top.mas_equalTo(self.IconImage);
    }];
    [fourView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(thereView.mas_right).offset(40);
        make.top.mas_equalTo(self.IconImage);
    }];

    [self.editorBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(oneView.mas_left);
        make.top.mas_equalTo(oneView.mas_bottom).offset(13);
        make.right.mas_equalTo(twoView.mas_right);
        make.height.mas_equalTo(24);
    }];
    [self.applyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(thereView.mas_left);
        make.top.mas_equalTo(oneView.mas_bottom).offset(13);
        make.right.mas_equalTo(fourView.mas_right);
        make.height.mas_equalTo(24);
    }];
    [sexView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(self.IconImage.mas_bottom).offset(20);
        make.right.mas_equalTo(-16);
    }];
    [sevenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(sexView.mas_bottom).offset(5);
        make.right.mas_equalTo(-16);
    }];
    [eightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(sevenView.mas_bottom).offset(5);
        make.right.mas_equalTo(-16);
    }];

    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(8);
    }];
}

-(UIImageView*)IconImage{
    if(!_IconImage){
        _IconImage=[UIImageView new];
        _IconImage.image = IMAGE_NAMED(@"userIconDefut");
        _IconImage.image = [Utilis getRandomUserImage];
        ViewRadius(_IconImage, 36);
    }
    return _IconImage;
}
-(UIView *)createButtonView:(NSString *)titleName label:(UILabel *)label{
    UIView * view = UIView.new;
    
    UILabel * titleLabel = UILabel.new;
    titleLabel.textColor = UIColorFromRGB(0x959E97);
    titleLabel.font = SYSTEMFONT(12);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = titleName;
    
    [view addSubview:titleLabel];
    [view addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.left.mas_equalTo(0);
        make.top.mas_equalTo(label.mas_bottom).offset(5);
    }];

    return view;
}
-(UIView *)createImageAndTitleView:(NSString *)imageName LabelName:(UILabel *)label{
    UIView * view = UIView.new;
    
    UIImageView * image = [UIImageView new];
    image.backgroundColor = KGrayColor;
    
    label.textColor = UIColorFromRGB(0x454F44);
    label.font = SYSTEMFONT(12);
    
    [view addSubview:image];
    [view addSubview:label];
    
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
    }];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(image.mas_right).offset(4);
        make.top.bottom.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
    }];

    return view;
}
-(UILabel*)oneNumLabel{
    if(!_oneNumLabel){
        _oneNumLabel=[UILabel new];
        _oneNumLabel.textColor = UIColorFromRGB(0x273323);
        _oneNumLabel.font = BOLDSYSTEMFONT(16);
        _oneNumLabel.textAlignment = NSTextAlignmentCenter;
        _oneNumLabel.text = @"100";
    }
    return _oneNumLabel;
}
-(UILabel*)twoNumLabel{
    if(!_twoNumLabel){
        _twoNumLabel=[UILabel new];
        _twoNumLabel.textColor = UIColorFromRGB(0x273323);
        _twoNumLabel.font = BOLDSYSTEMFONT(16);
        _twoNumLabel.textAlignment = NSTextAlignmentCenter;
        _twoNumLabel.text = @"2.2w";
    }
    return _twoNumLabel;
}
-(UILabel*)thereNumLabel{
    if(!_thereNumLabel){
        _thereNumLabel=[UILabel new];
        _thereNumLabel.textColor = UIColorFromRGB(0x273323);
        _thereNumLabel.font = BOLDSYSTEMFONT(16);
        _thereNumLabel.textAlignment = NSTextAlignmentCenter;
        _thereNumLabel.text = @"100";
    }
    return _thereNumLabel;
}
-(UILabel*)fourNumLabel{
    if(!_fourNumLabel){
        _fourNumLabel=[UILabel new];
        _fourNumLabel.textColor = UIColorFromRGB(0x273323);
        _fourNumLabel.font = BOLDSYSTEMFONT(16);
        _fourNumLabel.textAlignment = NSTextAlignmentCenter;
        _fourNumLabel.text = @"2.2w";
    }
    return _fourNumLabel;
}

@end
